/*
 * date: 2021/4/10
 * author: 2012190218 WangChenghua
 * linked with listlink.h
 * --std c++14, MSVC.
 * this file for student system.
 * */
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include "listlink.h"
using namespace std;

void Student::Readfile() {
    long long cnt = 0;
    InitList();
    ifstream  Myfile;
    Myfile.open("D:/Cexercise/clion_solution/sample.in");
    string i_name,i_number,i_s_class;
    int i_gender;
    while(Myfile>>i_name>>i_number>>i_s_class>>i_gender){
        Basic_insert_at_tail(i_name,i_number,i_s_class,i_gender);
        cnt++;
    }
    cout<<"Read "<<cnt<<" items."<<endl;
    Myfile.close();
}

void Student::Writefile() {
    ofstream  Myfile;
    Myfile.open("D:/Cexercise/clion_solution/stu_out.out");
    student *tmp = head;
    if(!tmp||!tmp->next){
        Myfile<<"empty"<<endl;
        return;
    }
    tmp = tmp->next;
    while(tmp){
        Myfile<<tmp->name<<" ";
        Myfile<<tmp->number<<" ";
        Myfile<<tmp->s_class<<" ";
        Myfile<<tmp->gender<<endl;
        tmp = tmp->next;
    }
    Myfile.close();
}

void Student::IO_Insert() {
    cout<<">>Enter Name, Number, Class, gender:"<<endl;
    cout<<">>gender(0 for male,1 for female!!!)"<<endl;
    cout<<">>";
    cin>>buf_name>>buf_number>>buf_class>>buf_gender;
}

void Student::InitList() {
    head = new student;
    head->next = nullptr;
    head->gender = 0; // head.data to count the node of this list;
    tails = head;
}

void Student::Insert_at_tail() {
    IO_Insert();
    Basic_insert_at_tail(buf_name,buf_number,buf_class,buf_gender);
}

void Student::Basic_insert_at_tail(string &a, string &b, string &c, int &d) {
    struct student *tmp = new student;
    tmp->next = nullptr;
    tmp->name = a;
    tmp->number = b;
    tmp->s_class = c;
    tmp->gender = d;
    tails->next = tmp;
    tails = tmp;
    head->gender++;
}

void Student::Print() {
    student *tmp = head;
    if(!tmp||!tmp->next){
        cout<<"empty"<<endl;
        return;
    }
    tmp = tmp->next;
    while(tmp){
        cout<<"------------------\n";
        cout<<"name:"<<tmp->name<<endl;
        cout<<"number:"<<tmp->number<<endl;
        cout<<"Class:"<<tmp->s_class<<endl;
        cout<<"Gender:";
        if(tmp->gender)cout<<"female\n";
        else cout<<"male\n";
        cout<<"------------------\n";
        tmp = tmp->next;
    }
}

void Student::Find() {
    cout<<">>Enter student number to find,q for quit."<<endl;
    string buff;
    while(cin>>buff && buff[0]!='q'){
        student *tmp = head;
        int count = 1;
        tmp = tmp->next;
        while(tmp && tmp->number!=buff){
            tmp = tmp->next;
            count++;
        }
        if(tmp && tmp->number==buff){
            cout<<"------------------\n";
            cout<<"name:"<<tmp->name<<endl;
            cout<<"number:"<<tmp->number<<endl;
            cout<<"Class:"<<tmp->s_class<<endl;
            cout<<"Gender:";
            if(tmp->gender)cout<<"female\n";
            else cout<<"male\n";
            cout<<"------------------\n";
        }else{
            cout<<"Not exist\n";
        }
    }
}

void Student::Delete() {
    cout<<">>Enter student number to delete,q for quit."<<endl;
    string buff;
    while(cin>>buff && buff[0]!='q'){
        student *tmp,*pre,*cnt;
        pre = head,tmp = head->next;
        while(tmp && tmp->number != buff){
            tmp = tmp->next;
            pre = pre->next;
        }
        if(tmp && tmp->number == buff){
            cnt = tmp;
            pre->next = cnt->next;
            head->gender--;
            delete cnt;
            cout<<"Deleted!"<<endl;
        }else{
            cout<<"Can't delete\n";
        }
    }
}


